import $store from '@/store/index.js';
import groupSqlite from "@/sqlite_services/group.js"
import groupMembersSqlite from '@/sqlite_services/group_members.js'
//邀人入群
const handle = async(data)=>{
	try{
		const activeChatUuid = $store.state.chat.chatInfo && $store.state.chat.chatInfo.uuid || ''; // 访问根级状态的chatInfo
		let myid = $store.state.user.user_info.user_id
		let isInclude = data.invite_ids.includes(myid)
		if(isInclude){
			//需要更新的字段
			let updata = {
				group_id:data.target_id,
				mygroup:'1'
			}
			//避免之前退过群,而还有会话列表，并且正在会话内
			$store.commit('updateChatGroupInfo',updata)
			//数据库是否有这个群，如果有就更新
			let groupsql = await groupSqlite.getGroupInfo(data.target_id)
			if(groupsql){
				await groupSqlite.editGroupInfo(data.target_id,updata)
			}
			if(activeChatUuid == data.target_id){
				//链接打开如果在群页面更新下配置
				uni.$emit('chatOperation',{type:'grouprefresh'})
			}
		}
		// let addData = []
		// data.invite_ids.forEach(item=>{
		// 	addData.push({
		// 		group_id:data.target_id,
		// 		user_id:item,
		// 		role:'member'
		// 	})
		// })
		if(activeChatUuid == data.target_id){
			// let newData = JSON.parse(JSON.stringify(addData))
			// const batchSize = 10; // 每批处理的数量，根据实际情况调整
			// let updatedMembers = [];
						
			// // 分批处理数据
			// for (let i = 0; i < newData.length; i += batchSize) {
			// 	const batch = newData.slice(i, i + batchSize);
			// 	let promises = batch.map(async (item) => {
			// 		let userinfo = await $store.dispatch('fetchUserInfo',{ userId: item.user_id });
			// 		item.group_nickname = ''
			// 		item.user_nickname = userinfo.nick_name;
			// 		item.avatar_url = userinfo.avatar_url
			// 		item.user_type = userinfo.user_type
			// 		item.role = 'member'
			// 		item.is_muted = '0'
			// 		delete item.group_id
			// 		return item;
			// 	});
			// 	let batchResults = await Promise.all(promises);
			// 	updatedMembers = updatedMembers.concat(batchResults);
			// }
			//发送全局通知,通知页面删除好友
			uni.$emit('onGroupMemberAction',{
				type:'addMember',
				data:{
					group_id:data.target_id,
					user_ids:data.invite_ids
				}
			})
		}
		// await groupMembersSqlite.batchReplaceMembers(addData)
	}catch(e){
		
	}
}
export default handle